package com.antoco.controlcenter.utils;

import android.content.Context;
import android.util.Xml;

import com.antoco.controlcenter.base.Constants;
import com.antoco.controlcenter.entity.ButtonEntity;
import com.antoco.controlcenter.entity.ConfigEntity;
import com.antoco.controlcenter.entity.ConnectEntity;
import com.antoco.controlcenter.entity.DisplayEntity;
import com.antoco.controlcenter.entity.GroupEntity;
import com.antoco.controlcenter.entity.InstructEntity;
import com.antoco.controlcenter.entity.OperatorsEntity;
import com.antoco.controlcenter.entity.ParserEntity;
import com.antoco.controlcenter.entity.PropertyEntity;
import com.antoco.controlcenter.entity.SliderEntity;
import com.antoco.controlcenter.entity.VideoEntity;


import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class XmlPullParserUtil {

    //pull解析
    public static ConfigEntity xmlPullParser(Context context, String nameXml) {
        XmlPullParser parser = Xml.newPullParser();
        InputStream is = null;
        ConfigEntity configEntity = Constants.CONFIG_MAP.get(nameXml);
        if (null != configEntity) {
            return configEntity;
        } else {
            configEntity = new ConfigEntity();
        }
        try {

            is = context.getResources().getAssets().open("config/" + nameXml);
            //添加文档信息进pull解析器
            parser.setInput(is, "UTF-8");
            //得到节点信息
            int evtType = parser.getEventType();
            GroupEntity groupEntity = null;
            PropertyEntity propertyEntity = null;
            // 判断开始标识
            while (evtType != XmlPullParser.END_DOCUMENT) {
                switch (evtType) {
                    // 判断开始标识
                    case XmlPullParser.START_TAG:
                        String tag = parser.getName();
                        int count = parser.getDepth();

                        switch (tag) {
                            case "robot":
                                int attributeValue = parser.getAttributeCount();
                                String name = parser.getAttributeValue(null, "name");
                                String type = parser.getAttributeValue(null, "type");
                                break;
                            case "connects":
                                configEntity.setConnectList(new ArrayList<>());
                                break;
                            case "connect":
                                if (2 == count) {
                                    configEntity.setConnectEntity(getConnectEntity(parser));
                                } else if (3 == count) {
                                    configEntity.getConnectList().add(getConnectEntity(parser));
                                }
                                break;
                            case "videos":
                                configEntity.setVideoEntityList(new ArrayList<>());
                                break;
                            case "video":
                                VideoEntity videoEntity = new VideoEntity();
                                videoEntity.setName(parser.getAttributeValue(null, "name"));
                                videoEntity.setUrl(parser.getAttributeValue(null, "url"));
                                configEntity.getVideoEntityList().add(videoEntity);
                                break;
                            case "operators":
                                OperatorsEntity operatorsEntity = new OperatorsEntity();
                                operatorsEntity.setSliderList(new ArrayList<>());
                                operatorsEntity.setGroupList(new ArrayList<>());
                                configEntity.setOperatorsEntity(operatorsEntity);
                                break;
                            case "slider":
                                SliderEntity sliderEntity = new SliderEntity();
                                sliderEntity.setName(parser.getAttributeValue(null, "name"));
                                sliderEntity.setMax(parser.getAttributeValue(null, "max"));
                                sliderEntity.setMin(parser.getAttributeValue(null, "min"));
                                sliderEntity.setStep(parser.getAttributeValue(null, "step"));
                                sliderEntity.setTag(parser.getAttributeValue(null, "tag"));
                                sliderEntity.setValue(parser.getAttributeValue(null, "value"));
                                configEntity.getOperatorsEntity().getSliderList().add(sliderEntity);
                                break;
                            case "group":
                                groupEntity = new GroupEntity();
                                groupEntity.setName(parser.getAttributeValue(null, "name"));
                                groupEntity.setButtonList(new ArrayList<>());
                                configEntity.getOperatorsEntity().getGroupList().add(groupEntity);
                                break;
                            case "button":
                                ButtonEntity buttonEntity = new ButtonEntity();
                                buttonEntity.setName(parser.getAttributeValue(null, "name"));
                                buttonEntity.setTag(parser.getAttributeValue(null, "tag"));
                                groupEntity.getButtonList().add(buttonEntity);
                                break;
                            case "properties":
                                configEntity.setPropertyList(new ArrayList<>());
                                break;
                            case "property":
                                propertyEntity = new PropertyEntity();
                                propertyEntity.setName(parser.getAttributeValue(null, "name"));
                                propertyEntity.setValue(parser.getAttributeValue(null, "value"));
                                propertyEntity.setType(parser.getAttributeValue(null, "type"));
                                configEntity.getPropertyList().add(propertyEntity);
                                break;
                            case "display":
                                DisplayEntity displayEntity = new DisplayEntity();
                                displayEntity.setTemplate(parser.getAttributeValue(null, "template"));
                                propertyEntity.setDisplayEntity(displayEntity);
                                break;
                            case "parser":
                                ParserEntity parserEntity = new ParserEntity();
                                parserEntity.setAccuracy(parser.getAttributeValue(null, "accuracy"));
                                parserEntity.setCorrect(parser.getAttributeValue(null, "correct"));
                                parserEntity.setLength(parser.getAttributeValue(null, "length"));
                                parserEntity.setMask(parser.getAttributeValue(null, "mask"));
                                parserEntity.setOffset(parser.getAttributeValue(null, "offset"));
                                parserEntity.setType(parser.getAttributeValue(null, "type"));
                                parserEntity.setMeta(parser.getAttributeValue(null, "meta"));
                                propertyEntity.setParserEntity(parserEntity);
                                break;
                            case "instructions":
                                configEntity.setInstructionsList(new ArrayList<>());
                                break;
                            case "instruct":
                                InstructEntity instructEntity = new InstructEntity();
                                instructEntity.setName(parser.getAttributeValue(null, "name"));
                                instructEntity.setValue(parser.nextText());
                                configEntity.getInstructionsList().add(instructEntity);
                                break;
                        }

                        break;
                    // 判断结束标识
                    case XmlPullParser.END_TAG:
                        // 判断结束标识
                        if ("robot".equals(parser.getName())) {

                        }
                        break;
                }
                // 解析下一个标签
                evtType = parser.next();
            }
            Constants.CONFIG_MAP.put(nameXml, configEntity);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return configEntity;
    }

    private static ConnectEntity getConnectEntity(XmlPullParser parser) {
        ConnectEntity connectEntity = new ConnectEntity();
        try {
            connectEntity.setType(parser.getAttributeValue(null, "type"));
            connectEntity.setName(parser.getAttributeValue(null, "name"));
            String[] ipPorts = parser.nextText().split(":");
            if (ipPorts.length >= 1) {
                connectEntity.setIp(ipPorts[0]);
            }
            if (ipPorts.length >= 2) {
                connectEntity.setPort(ipPorts[1]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        return connectEntity;

    }
}
